/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.cognition.util.j2me;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Abstract class used for logging.  This logging class is thread consciencous,
 * using one thread for the entire system.  Logging events are scheduled
 * on the thread for being handled using the same policy as TimerTask objects.
 * Logging events are in fact run as TimerTask objects.
 *
 * @author richard
 */
public abstract class Logger{
    private static Timer _timer = new Timer();

    private String _name;

    
    protected Logger(String name){
            _name = name;
    }

    /**
     * Logs a message at the debug level. Ignored if SystemLogger.getCurrentLevel
     * is not at or higher than LOG_LEVEL_DEBUG
     * @param tag the tag to output
     * @param msg the object to use as the message
     */
    public void debug(String tag, Object msg){
        at_level(SystemLogger.LOG_LEVEL_DEBUG, tag, msg);
    }

    /**
     * Logs a message at the info level. Ignored if SystemLogger.getCurrentLevel
     * is not at or higher than LOG_LEVEL_INFO
     * @param tag the tag to output
     * @param msg the object to use as the message
     */
    public void info(String tag, Object msg){
        at_level(SystemLogger.LOG_LEVEL_INFO, tag, msg);
    }

    /**
     * Logs a message at the error level. Ignored if SystemLogger.getCurrentLevel
     * is not at or higher than LOG_LEVEL_ERROR
     * @param tag the tag to output
     * @param msg the object to use as the message
     */
    public void error(String tag, Object msg){
        at_level(SystemLogger.LOG_LEVEL_ERROR, tag, msg);

    }
    /**
     * Logs a message at the warn level. Ignored if SystemLogger.getCurrentLevel
     * is not at or higher than LOG_LEVEL_WARN
     * @param tag the tag to output
     * @param msg the object to use as the message
     */
    public void warn(String tag, Object msg){
        at_level(SystemLogger.LOG_LEVEL_WARN, tag, msg);

    }

    /**
     * Logs a message at the critical level. Ignored if SystemLogger.getCurrentLevel
     * is not at or higher than LOG_LEVEL_CRITICAL
     * @param tag the tag to output
     * @param msg the object to use as the message
     */
    public void critical(String tag, Object msg){
        at_level(SystemLogger.LOG_LEVEL_CRITICAL, tag, msg);
    }

    /**
     * Logs a message at the specified level. Never ignored.
     * is not at or higher than LOG_LEVEL_DEBUG
     * @param tag the tag to output
     * @param msg the object to use as the message
     */
    public void at_level(int level,  String tag, Object msg){
        if (!SystemLogger.isValidLevel(level))
            throw new IllegalArgumentException("not valid logging level.");


        if (SystemLogger.getCurrentLevel() <= level)
            _timer.schedule(new LogTask(level, tag, msg), 0);
    }


    /**
     * Override this to provide custom logging handling.
     * @param level
     * @param name
     * @param tag
     * @param msg
     */
    public abstract void log(int level, String name, String tag, Object msg);

    private class LogTask extends TimerTask{

        private String _tag;
        private Object _msg;
        private int _level;

        public LogTask(int level, String tag, Object msg){
            _level = level;
            _tag = tag;
            _msg = msg;
        }
        public void run() {
            log(_level, _name, _tag, _msg);
        }

    }
}
